﻿
@{
    ViewBag.Title = "添加商品分类";
    Layout = "~/Views/Shared/DetailsInfoLayout.cshtml";
}

@section CSS{
    <link href="~/Content/css/layerui/layui.css" rel="stylesheet" />
    <link href="~/Content/js/UploadImg/css/webuploader.css" rel="stylesheet" />
}
<section class="content">
    <div class="row">
        <div class="col-md-9">
            <div class="ibox-content">
                <form class="form-horizontal" id="form-add">
                    <input type="hidden" id="id" name="id" value="0" />
                    <input type="hidden" id="parent_id" name="parent_id" value="@ViewBag.id" />
                    <input type="hidden" id="type" name="type" value="@ViewBag.tid" />
                    <div class="form-group">
                        <label class="col-sm-3 control-label">分类名称：</label>
                        <div class="col-sm-9">
                            <input type="text" name="name" id="name" required="" aria-required="true" class="form-control" placeholder="请输入分类名称" />
                            <span class="help-block m-b-none"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">分类图片：</label>
                        <div class="col-sm-9">
                            <div class="uploader-thum-container">
                                <input type="hidden" value="" placeholder="" id="icon" name="icon">
                                <div class="upload">
                                    <div class="uploadPreview" style="border:1px solid #cfcfcf;width:120px;height:120px;line-height:120px;text-align:center;">请上传</div>
                                    <div class="left">
                                        <div class="uploadProgressBar"></div>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div class="uploadBut" style="margin:-50px 0 0 130px;">选择图片</div>
                                @*<p style="color:#333;">图片宽高比例为1:1</p>*@
                            </div>
                        </div>
                    </div>
                    <div class="form-group" id="s_rate" >
                        <label class="col-sm-3 control-label">费率%：</label>
                        <div class="col-sm-9">
                            <input type="text" name="service_rate" id="service_rate" required="" value="0" number="true" aria-required="true" class="form-control" placeholder="请输入费率" />
                            <span class="help-block m-b-none"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排序：</label>
                        <div class="col-sm-9">
                            <input type="text" name="sort" id="sort" required="" value="99" digits="true" aria-required="true" class="form-control" placeholder="请输入分类排序" />
                            <span class="help-block m-b-none"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label"></label>
                        <div class="col-sm-9">
                            <label><input type="checkbox" name="status" id="status" value="true" checked="checked" class="i-checks">是否启用</label> &nbsp;&nbsp;
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-3">
                            <button class="btn btn-primary" type="submit">保存内容</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
@section JS{
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Content/plugins/validate/messages_zh.min.js"></script>
    <script src="~/Content/js/UploadImg/js/webuploader.js"></script>

    <script type="text/javascript">
        var _id = @ViewBag.id;
        var _tid = @ViewBag.tid;
        $(function () {
            if (_tid != undefined&&_tid==0) {
                getinfo();
            }
            if (_tid==1) {
                $("#s_rate").val("20");
            }
            if (_tid==2) {
                $("#s_rate").val("0").hide();
            }
            $("#status").change(function() {
                var obj=$(this);
                if(obj.is(':checked')){obj.val("true");}
                else{obj.val("false");}
            });

        })

        function getinfo() {
            ajax("/SystemSetting/GoodsCategoryInfo/"+_id, {}, function (data) {
                if (data!="") {
                    $("#id").val(data.id);
                    if (data.icon != null && data.icon!="") {
                        $(".uploadPreview").html("<img style='width:120px;height:120px;' src=\""+domain + data.icon + "\"  />");
                        $("#icon").val(data.icon);
                    }
                    $("#sort").val(data.sort);
                    $("#parent_id").val(data.parent_id);
                    $("#type").val(data.type);
                    if (data.type==2) {
                        $("#s_rate").val("0").hide();
                    }
                    $("#name").val(data.name);
                    $("#service_rate").val(data.service_rate);
                    $("#remark").val(data.remark);
                    if (data.status !=1) {
                        $("#status").removeAttr("checked").val("false");
                    }
                };
            }, true, "", "get");
        }


        $("#form-add").validate({
            ajaxPost: true,
            submitHandler: function (form) {
                infosubmit();
            }
        });

        var flag = true;
        function infosubmit() {
            if (flag) {
                $("#form-add").ajaxSubmit({
                    url: "/SystemSetting/SaveCategory", type: "POST",
                    dataType: "json",
                    success: function (result) {
                        if (result.status == "y") {
                            parent.layer.msg("操作成功", { time: 1200 }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.replace(parent.location.href);
                                parent.layer.close(index);
                            });
                        }
                        else {
                            parent.layer.msg(result.info, { time: 2000 });
                            flag = true;
                        }
                    }
                });
            }
        }

        $(function () {

            var uploader = WebUploader.create({
                pick: '.uploadBut',
                auto: true,
                swf: '~/Content/js/UploadImg/js/Uploader.swf',

                // 文件接收服务端。
                server:domain+ "/tools/UploadPCImage?type=Category",
                fileVal: 'upload',
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                //resize: false,
                // 只允许选择图片文件。
                accept: {
                    title: 'Images',
                    extensions: '*',
                    mimeTypes: 'image/*'
                }
            });
            uploader.on('uploadProgress', function (file, percentage) {
                $(".uploadPreview").html("上传中...");
            });

            uploader.on('uploadSuccess', function (file, response) {
                var data = response.info;
                if (data == undefined) {
                    data = response;
                }
                if (data == "-1")
                    $(".uploadPreview").html('图片不能为空');
                else if (data == "-2")
                    $(".uploadPreview").html('图片类型不允许');
                else if (data == "-3")
                    $(".uploadPreview").html('图片太大');
                else {
                    $(".uploadPreview").html("<img style='width:120px;height:120px;' src=\""+domain + data + "\"  />");
                    //$("#ShowImg").val(data);
                    $('#icon').val(data);
                }
            });

            uploader.on('uploadError', function (file) {
                //alert(3);
                $(".uploadPreview").html("上传失败");
            });
        })
    </script>
}

